package com.kll.tv.views.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.kll.carolinehmibiz.net.bean.GetCalllogResponse
import com.kll.tv.R
import com.kll.tv.databinding.ItemMyAppointmentBinding

class MyAppointmentAdapter(var context: Context) : RecyclerView.Adapter<MyAppointmentAdapter.InnerHolder>() {
    private var mItemClickListener: OnAppointmentListItemClickListener? = null

    // 数据
    var data: MutableList<GetCalllogResponse.CallLog> = ArrayList()
        set(value) {
            if (value == null) {
                data.clear()
            } else {
                field = value
            }
            notifyDataSetChanged()
        }

    fun addData(data: ArrayList<GetCalllogResponse.CallLog>) {
        if (data != null) {
            this.data.addAll(data)
            notifyDataSetChanged()
        }
    }

    inner class InnerHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var binding = ItemMyAppointmentBinding.bind(itemView)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): InnerHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_my_appointment, parent, false)
        return InnerHolder(view)
    }

    override fun onBindViewHolder(holder: InnerHolder, position: Int) {
        val data = data.get(position)

        // item可以获得焦点，需要设置这个属性。
        holder.itemView.setFocusable(true)

        holder.binding.tvServiceName.text = data.staff_name
        holder.binding.tvServiceTime.text = data.end_time

        if (mItemClickListener != null) {
            holder.itemView.setOnClickListener {
                mItemClickListener!!.onAppointmentItemClick(data)
            }
        }
    }

    override fun getItemCount(): Int {
        return data.size
    }

    interface OnAppointmentListItemClickListener {
        fun onAppointmentItemClick(data: GetCalllogResponse.CallLog)
    }

    fun setOnAppointmentListItemClickListener(listener: OnAppointmentListItemClickListener) {
        this.mItemClickListener = listener
    }

}